/**
 * 难点：
 * 1.如何配置alias,如何解决import {resolve} from "path" ts报错
 */
import { ConfigEnv, loadEnv, defineConfig, UserConfig} from 'vite'
import react from '@vitejs/plugin-react'
import {resolve} from "path"
import {createHtmlPlugin} from "vite-plugin-html"

// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv):UserConfig => {
  const env = loadEnv(mode.mode, process.cwd())
  let port = parseInt(env.VITE_PORT)

  return {
    resolve: {
      alias: {
        '@':resolve(__dirname, "./src")
      }
    },
    css: {
      preprocessorOptions:{
        less:{
          javascriptEnabled: true,
          additionalData:`@import "@/styles/var.less";` //这里的分号不能落下，不然分分钟报错给你看
        }
      }
    },
    server: {
      host: "0.0.0.0", // 服务器主机名，如果允许外部访问，可设置为“0.0.0.0”，
      port: port,
      //open: env.VITE_OPEN,
      cors: true,
      proxy:{
        "/api": {
					target: "https://mock.mengxuegu.com/mock/641c67b7e24b4b4cfeac9ddf", // easymock
					changeOrigin: true,
					rewrite: path => path.replace(/^\/api/, "")
				}
      }
    },
    plugins: [
      react(),
      createHtmlPlugin({
        inject: {
          data: {
            title: env.VITE_GLOB_APP_TITLE
          }
        }
      })
    ],
    base:'./',
    build: {
      outDir: "dist",
      minify: "terser",
      rollupOptions: {
        output: {
          chunkFileNames: "assets/js/[name]-[hash].js",
					entryFileNames: "assets/js/[name]-[hash].js",
					assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
        }
      }
    }
  }
})
